package cn.com.huak.www.sell.controller;

import cn.com.huak.www.sell.dataObject.ProductCategory;
import cn.com.huak.www.sell.form.CategoryForm;
import cn.com.huak.www.sell.service.ProductCategoryService;
import cn.com.huak.www.sell.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;



/**
 * 卖家端类目
 * @author huak@wanggang
 * @date 2019/2/24 21:22
 */
@Controller
@Slf4j
@RequestMapping("/seller/category")
public class SellerCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 类目列表
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(Model model){
        List<ProductCategory> all = productCategoryService.findAll();
        model.addAttribute("categoryList", all);
        return "category/list";
    }

    /**
     * 新增/修改页面
     * @param categoryId
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(@RequestParam(value="categoryId", required = false) String categoryId,
                        Model model){
        if(!StringUtils.isEmpty(categoryId)){
            ProductCategory one = productCategoryService.findOne(categoryId);
            model.addAttribute("category", one);
        }
        return "category/index";
    }

    /**
     * 类目信息保存
     * @param categoryForm 类目表单
     * @return
     */
    @PostMapping("/save")
    public String save(@Valid CategoryForm categoryForm,
                       BindingResult bindingResult,
                       Model model){
        //1. 参数校验
        if(bindingResult.hasErrors()){
            model.addAttribute("msg", bindingResult.getFieldError().getDefaultMessage());
            model.addAttribute("url", "/sell/seller/category/list");
            return "common/error";
        }

        //2. 属性复制
        try{
            ProductCategory productCategory = new ProductCategory();
            if(!StringUtils.isEmpty(categoryForm.getId())){
                productCategory = productCategoryService.findOne(categoryForm.getId());
            }
            BeanUtils.copyProperties(categoryForm, productCategory);
            if(StringUtils.isEmpty(categoryForm.getId())){
                productCategory.setId(KeyUtil.genKey());
            }

            //3. 信息持久化
            productCategoryService.save(productCategory);
        }catch (Exception e){
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url", "/sell/seller/category/list");
            return "common/error";
        }
        model.addAttribute("msg", "操作成功");
        model.addAttribute("url", "/sell/seller/category/list");
        return "common/success";
    }
}
